package cn.iocoder.yudao.module.pplan.controller.admin.recipeaddrecord;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.recipeaddrecord.vo.RecipeAddRecordPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.recipeaddrecord.vo.RecipeAddRecordRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.recipeaddrecord.vo.RecipeAddRecordSaveReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.recipeaddrecord.RecipeAddRecordDO;
import cn.iocoder.yudao.module.pplan.service.recipeaddrecord.RecipeAddRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 生产计划-加餐记录")
@RestController
@RequestMapping("/pplan/recipe-add-record")
@Validated
public class RecipeAddRecordController {

    @Resource
    private RecipeAddRecordService recipeAddRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建生产计划-加餐记录")
    @PreAuthorize("@ss.hasPermission('pplan:recipe-add-record:create')")
    public CommonResult<Long> createRecipeAddRecord(@Valid @RequestBody RecipeAddRecordSaveReqVO createReqVO) {
        return success(recipeAddRecordService.createRecipeAddRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产计划-加餐记录")
    @PreAuthorize("@ss.hasPermission('pplan:recipe-add-record:update')")
    public CommonResult<Boolean> updateRecipeAddRecord(@Valid @RequestBody RecipeAddRecordSaveReqVO updateReqVO) {
        recipeAddRecordService.updateRecipeAddRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产计划-加餐记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pplan:recipe-add-record:delete')")
    public CommonResult<Boolean> deleteRecipeAddRecord(@RequestParam("id") Long id) {
        recipeAddRecordService.deleteRecipeAddRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除生产计划-加餐记录")
                @PreAuthorize("@ss.hasPermission('pplan:recipe-add-record:delete')")
    public CommonResult<Boolean> deleteRecipeAddRecordList(@RequestParam("ids") List<Long> ids) {
        recipeAddRecordService.deleteRecipeAddRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产计划-加餐记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pplan:recipe-add-record:query')")
    public CommonResult<RecipeAddRecordRespVO> getRecipeAddRecord(@RequestParam("id") Long id) {
        RecipeAddRecordDO recipeAddRecord = recipeAddRecordService.getRecipeAddRecord(id);
        return success(BeanUtils.toBean(recipeAddRecord, RecipeAddRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产计划-加餐记录分页")
    @PreAuthorize("@ss.hasPermission('pplan:recipe-add-record:query')")
    public CommonResult<PageResult<RecipeAddRecordRespVO>> getRecipeAddRecordPage(@Valid RecipeAddRecordPageReqVO pageReqVO) {
        PageResult<RecipeAddRecordDO> pageResult = recipeAddRecordService.getRecipeAddRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecipeAddRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产计划-加餐记录 Excel")
    @PreAuthorize("@ss.hasPermission('pplan:recipe-add-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecipeAddRecordExcel(@Valid RecipeAddRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecipeAddRecordDO> list = recipeAddRecordService.getRecipeAddRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产计划-加餐记录.xls", "数据", RecipeAddRecordRespVO.class,
                        BeanUtils.toBean(list, RecipeAddRecordRespVO.class));
    }

}